home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / hsend.exe / BHSEND.C < prev    next >
Text File  |  1992-01-17  |  20KB  |  778 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5. #include <stat.h>
  6. #include <conio.h>
  7. #include <dir.h>
  8. #include <dos.h>
  9. #include <time.h>
  10. #include <async.h>
  11. #include <process.h>
  12.  
  13. #define HOST 1
  14. #define GUEST 0
  15. #define DATAFILE "CONFIGUR.DAT"
  16. #define OFFLINE (!(a_getmsr(P)&CRX) && !(bioskey(2)&64))
  17. #define TRY 3                    /* number of attempts to resend garbled data block */
  18. #define CRX 128                    /* carrier detect        */
  19. #define OKTS 1                    /* OK to send            */
  20. #define SFR 2                    /* send file request     */
  21. #define IFR 3                    /* initiate file receive */
  22. #define IFS 4                   /* initiate file send    */
  23. #define DF 5                    /* delete file           */
  24. #define RP 6                    /* request password        */
  25. #define SP 7                    /* sending password        */
  26. #define EM 8                    /* error message         */
  27. #define EOC 9                    /* end of call            */
  28. #define ACK 10                    /* acknowledgement        */
  29. #define NAK 11                    /* not acknowledged        */
  30. #define SDB 12                    /* sending data block    */
  31.  
  32. void host_mode(void),beep(int,int),copyright(void),report(char *);
  33. void delete_files(char *,int),error_message(char *),end_of_call(void);
  34. void empty_box(int,int,int,int),tsr(unsigned int),b_sleep(int);
  35. void interrupt background_host_mode(void);
  36. void interrupt call(void);
  37. int initiate_file_send(char *,char *),send_file_request(unsigned char *,int);
  38. int init_modem(void),load_data(void),break_handler(void);
  39. int password_check(void),scb(int,int,unsigned char *),rcb(int,unsigned char *),agetc(void);
  40. int cmd_mode(int),seperate(char *,char *,char*,char);
  41. int create_local_file(char *),rdb(int,unsigned char *);
  42. char *eight_plus_three(char *,char);
  43.  
  44. int WAITING,BAUD,COMM,RINGS,SCREENFLAG,LOGFILEFLAG,FLAGFILEFLAG;
  45. int I,C,TIMEOUT=180,IN_USE=0;
  46. long TIMER;
  47. char LOGFILE[61],FLAGFILE[61],PASSWORD[13];
  48. unsigned char TEMP[518],BUF[518],IOBUF[518];
  49. char SOURCE_PATHNAME[200],DEST_PATHNAME[200],FROM[200],TO[200];
  50. char PATH[100],PATHNAME[116];
  51. char SCREEN_BUFFER[644],RESTORE_BUFFER[644];
  52. struct ffblk F,F1;
  53.  
  54. ASYNC *P=NULL;
  55.  
  56. struct address{
  57.     char far *p;};
  58.  
  59. struct address far *int63=(struct address far *) 252;
  60. struct address far *int62=(struct address far *) 248;
  61. struct address far *int28h=(struct address far *) 160;
  62.  
  63. struct tm *PTR;
  64. time_t LT;
  65.  
  66. main(argc)
  67. int argc;
  68. {
  69. load_data();
  70.  
  71. if(argc>1){
  72.     cprintf("\n\rUsage:  bhsend <no arguments>    Run in background host mode\n\r");
  73.     beep(3,400);
  74.     exit(1);}
  75.  
  76.  
  77. if(int63->p!=int62->p){
  78.     cprintf("\n\rBHSEND is already running.\n\r");
  79.     exit(1);}
  80.  
  81.  
  82. TIMEOUT*=2;                        /* host waits longer */
  83. copyright();
  84.  
  85. if(!init_modem()){
  86.     gotoxy(1,21);
  87.     textcolor(LIGHTGRAY);
  88.     cprintf("\n\rCOMM device did not respond.  Check settings with CONFIGUR.\n\r");
  89.     beep(3,400);
  90.     exit(1);}
  91.  
  92.  
  93. gotoxy(22,12);
  94. textcolor(LIGHTCYAN|BLINK);
  95. cprintf("Background program is running, please wait");
  96. gettext(20,8,65,14,SCREEN_BUFFER);
  97. gotoxy(22,12);
  98. cprintf("                                          ");
  99. gotoxy(1,24);
  100. textcolor(LIGHTGRAY);
  101. cprintf("HSEND background mode has been loaded into memory.");
  102. int62->p=int28h->p;                         /* move the old vector   */
  103. int28h->p=(char far *)background_host_mode;            /* put in the new vector */
  104. tsr(5473);
  105. }
  106.  
  107.  
  108. void interrupt background_host_mode(void)                            /* TSR enters here */
  109. {
  110. geninterrupt(62);
  111.  
  112. if(!OFFLINE && !IN_USE){
  113.     IN_USE++;
  114.     gettext(20,8,65,14,RESTORE_BUFFER);
  115.     puttext(20,8,65,14,SCREEN_BUFFER);
  116.     LT=time(NULL);
  117.     PTR=localtime(<);
  118.     strcpy(TEMP,"On-line: ");
  119.     strcat(TEMP,asctime(PTR));
  120.     report(TEMP);
  121.     if(password_check()) while(cmd_mode(1));
  122.     LT=time(NULL);
  123.     PTR=localtime(<);
  124.     strcpy(TEMP,"Modem Reset: ");
  125.     strcat(TEMP,asctime(PTR));
  126.     report(TEMP);
  127.     report("---------------------------------------------------------------------------\n\r");
  128.     a_setmcr(P,0);                    /* hang up */                        /* set DTR and RTS high                    */
  129.     b_sleep(2);
  130.     a_setmcr(P,MCR_DTR+MCR_RTS);            /* set DTR and RTS high                    */
  131.     b_sleep(1);
  132.     a_puts("+++",P);
  133.     b_sleep(2);
  134.     a_puts("\n\rATZ\n\r",P);
  135.     b_sleep(2);
  136.     strcpy(TEMP,"AT S0=1 V0 &D3\n\r");
  137.     TEMP[6]=RINGS+48;                            /* set correct value for S0 */
  138.     a_puts(TEMP,P);
  139.     C=a_icount(P);
  140.     for(I=0;I<C;I++)
  141.         a_getc(P);
  142.     while(!OFFLINE);                            /* wait here */
  143.     while(bioskey(1)) bioskey(0);                    /* empty keystroke buffer */
  144.     puttext(20,8,65,14,RESTORE_BUFFER);
  145.     IN_USE=0;
  146.     }
  147.  
  148.  
  149. }                                        /* TSR exits until next int28H call */
  150.  
  151.  
  152. int cmd_mode(host_flag)                            /* return 0 at end of sesssion      */
  153. {
  154.  
  155. for(;;){
  156.  if(host_flag){
  157.     if(!scb(OKTS,0,NULL)){                        /* OK to send                       */
  158.         report("ERROR- OKTS transmission not acknowledged");
  159.         return 0;}}
  160.  
  161.  
  162.  do{                                            /* receiving command                */
  163.     if(OFFLINE){
  164.         report("DATA CONNECTION LOST");
  165.         return 0;}
  166.     C=rcb(0,BUF);
  167.     }while(!C);
  168.  
  169. switch(C){
  170.  
  171.     case OKTS: return -1;
  172.  
  173.     case SFR: send_file_request(BUF,0);
  174.             break;
  175.  
  176.     case IFR: create_local_file(BUF);
  177.             break;
  178.  
  179.     case DF:  delete_files(BUF,HOST);
  180.             break;
  181.  
  182.     case EM:  error_message(BUF);
  183.             break;
  184.  
  185.     case EOC: report("Transmission terminated normally");
  186.             return 0;
  187.  
  188.     default:  report("cmd_mode() default");
  189.             if(!host_flag) return -1;}            /* switch back to active mode */
  190.  
  191.  
  192. } /* endless loop */
  193. }
  194.  
  195.  
  196. void report(message)
  197. char *message;
  198. {
  199. int fp;
  200.  
  201. if(!LOGFILEFLAG) return;
  202.  
  203. if((fp=_open(LOGFILE,O_WRONLY|O_APPEND))==-1){
  204.     if((fp=open(LOGFILE,O_CREAT,S_IWRITE))==-1){
  205.          putch('!');
  206.          return;}}
  207.  
  208. if(write(fp,message,strlen(message))<0) beep(3,300);
  209. write(fp,"\n\r",2);
  210. close(fp);
  211. }
  212.  
  213.  
  214. int init_modem(void)
  215. {
  216. int x;
  217.  
  218. if(P) a_close(P,0);
  219. P=a_open(COMM,BAUD,0,8,1,1040,1040);                /* open at no parity,8 dbits, 1 sbit 1K input ,1k output buffer */
  220. a_setmcr(P,MCR_DTR+MCR_RTS);                        /* set DTR and RTS high                    */
  221. b_sleep(2);
  222. a_iflush(P);                                /* clear input buffer                      */
  223. b_sleep(1);
  224. a_puts("+++",P);
  225. b_sleep(2);
  226. a_puts("\n\rATZ\n\r",P);
  227. b_sleep(2);
  228. strcpy(TEMP,"AT s0=1 V0\n\r");
  229. TEMP[6]=RINGS+48;                            /* set correct value for S0 */
  230. a_puts(TEMP,P);
  231. a_waitquiet(P,18,72,1);                            /* wait for a reply                        */
  232. x=a_icount(P);
  233. while(a_icount(P)) a_getc(P);                        /* clear input buffer                      */
  234. return x;                                        /* return # of characters in reply         */
  235. }                                            /* 0 return value denotes no modem present */
  236.  
  237.  
  238. int agetc(void)                                /* returns 0 if error occurs     */
  239. {
  240.  
  241. TIMER=biostime(0,0);
  242. do{
  243.     if(OFFLINE) return -1;                        /* no carrier                    */
  244.     if(a_icount(P)) return a_getc(P);
  245.     }while(biostime(0,0)-TIMER<TIMEOUT);
  246.  
  247. report("Timeout");
  248. return -2;
  249. }
  250.  
  251.  
  252. int password_check(void)                            /* return 1 if password is verified */
  253. {
  254. b_sleep(3);
  255. if(!scb(RP,0,NULL)){
  256.     report("Caller could not verify Requesting Password transmission");
  257.     return 0;}
  258. if(!rcb(SP,BUF)){
  259.     report("Caller did not sent password");
  260.     return 0;}
  261. if(!strcmp(BUF,PASSWORD)) return 1;                /* correct password received */
  262. report("Incorrect password received.");
  263. return 0;
  264. }
  265.  
  266.  
  267. int scb(cmd,blklen,blk)                            /* send command block, returns 1 if acknowledged */
  268. int cmd;                                        /* #defined command code            */
  269. int blklen;                                    /* number of data characters        */
  270. unsigned char *blk;                                /* 0 to 512 data characters         */
  271. {                                            /* returns value of carrier detect  */
  272. int i,try,reply;
  273. unsigned long cksum;
  274.  
  275. while(a_icount(P)) a_getc(P);                        /* clear input buffer     */
  276. IOBUF[3]=cmd;                                    /* command code           */
  277. IOBUF[4]=blklen/255;                                /* # of data chars hibyte */
  278. IOBUF[5]=blklen-(IOBUF[4]*255);                        /* # of data chars lobyte */
  279.  
  280. if(blklen){                                    /* if there are data characters */
  281.     for(i=0;i<blklen;i++)                        /* copy them into buf           */
  282.         IOBUF[i+6]=blk[i];}
  283.  
  284. for(i=3,cksum=0;i<blklen+6;i++)                    /* add to create checksum */
  285.     cksum+=IOBUF[i];
  286.  
  287. IOBUF[0]=cksum/65025;                                /* checksum hibyte  */
  288. cksum-=(long)IOBUF[0]*65025;
  289. IOBUF[1]=cksum/255;                                /* checksum midbyte */
  290. IOBUF[2]=cksum-(long)(IOBUF[1]*255);                    /* checksum lobyte  */
  291.  
  292. for(try=0;try<TRY;try++){
  293.     for(i=0;i<blklen+6;i++)
  294.         a_putc(IOBUF[i],P);